package yang.java8.missingJava7;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Scanner;

import yang.java8.util.Util;

public class StudyJava7 {

	public static void main(String[] args) throws IOException, URISyntaxException {
		FilesStudy();
	}
	
	public static void TryWithResources() {
		InputStream input = StudyJava7.class.getResourceAsStream("in.txt");
		try (Scanner in = new Scanner(input);
				PrintWriter out = new PrintWriter("out.txt")) {
			while (in.hasNext()) {
				out.println(in.next().toLowerCase());
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void FilesStudy() throws IOException, URISyntaxException {
		Path path = Paths.get(StudyJava7.class.getResource("in.txt").toURI());
		// 一次性读取
		byte[] bytes = Files.readAllBytes(path);
		String content = new String(bytes, StandardCharsets.UTF_8);
		System.out.println(content);
		// 一次性按行读取
		List<String> lines = Files.readAllLines(path);
		Util.ShowList(lines);
		// 按内容写入
		Files.write(path, content.getBytes(StandardCharsets.UTF_8));
		// 按行写入
		Files.write(path, lines, StandardOpenOption.APPEND);
		
		InputStream inStream = Files.newInputStream(path);
		OutputStream outStream = Files.newOutputStream(path);
		Reader reader = Files.newBufferedReader(path);
		Writer writer = Files.newBufferedWriter(path);
	}

}
